package cn.phecda.boot.tenant.core.annotation;

import cn.phecda.boot.common.lang.constants.PropertyConstants;
import cn.phecda.boot.common.lang.model.TenantStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.lang.NonNull;

/**
 * <p> table租户条件</p>
 *
 * @author : wanpinwei
 */
@Slf4j
public class TableStrategyCondition implements Condition {
    @Override
    public boolean matches(@NonNull ConditionContext context, @NonNull AnnotatedTypeMetadata metadata) {
        String propertyName = PropertyConstants.PROPERTY_PREFIX_TENANT_STRATEGY;
        TenantStrategy propertyValue = TenantStrategy.fromEnvirment(context.getEnvironment(), propertyName, true);
        boolean matched = TenantStrategy.TABLE.equals(propertyValue);

        if (log.isDebugEnabled()) {
            log.debug("|Phecda Boot Tenant|- The property [{}={}], the condition value [{}], mathced? {}", propertyName,
                    propertyValue,
                    TenantStrategy.TABLE,
                    matched ? "Yes" : "No");
        }
        return matched;
    }
}
